package com.yitao.cms.service.shopInfo;


import com.google.zxing.WriterException;
import com.skipper.base.bean.CommonStatus;
import com.skipper.base.bean.DataOutput;
import com.skipper.base.json.JSON;
import com.skipper.base.json.JSONObject;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.shopInfo.ShopQrcodeDto;
import com.yitao.client.model.shopInfo.QShopQrcodeModel;
import com.yitao.client.model.shopInfo.ShopQrcodeModel;
import com.yitao.cms.constant.UserSession;
import com.yitao.cms.model.operationUser.OperationUserCmsModel;
import com.yitao.cms.utils.BaseUtils;
import com.yitao.cms.utils.QRCodeImageUtils;
import com.yitao.cms.utils.UpdateFileUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.Date;

/**
 * @Author: wwl
 * @Date: 2019/4/29 20:23
 * @Description:
 */
@Service
public class ShopQrcodeService extends GenericEntityService<ShopQrcodeModel, ShopQrcodeDto, QShopQrcodeModel> {
    public ShopQrcodeService(EntityManager entityManager) {
        super(entityManager);
    }

    @Autowired
    private QRCodeImageUtils qrCodeImageUtils;
    @Autowired
    private UpdateFileUtils updateFileUtils;

    /**
     * 根据shopID及url查看二维码信息
     *
     * @param shopId
     * @param url
     * @return
     */
    public ShopQrcodeModel findShopQRCodeByShopIdAndUrl(Long shopId, String url) {
        if (shopId != null && StringUtils.isNotBlank(url)) {
            return queryFactory.select(Q).from(Q).where(Q.shopId.eq(shopId).and(Q.url.eq(url)).and(Q.isDelete.eq(false))).fetchFirst();
        }
        return null;
    }

    /**
     * 保存二维码图片信息
     *
     * @param shopId
     * @param url
     * @param QRCode
     * @return
     */
    @Transactional
    public DataOutput saveShopQRCode(Long shopId, String url, String QRCode) {
        if (shopId != null && StringUtils.isNotBlank(url) && StringUtils.isNotBlank(QRCode)) {
            ShopQrcodeModel shopQrcodeModel = new ShopQrcodeModel();
            shopQrcodeModel.setShopId(shopId);
            shopQrcodeModel.setUrl(url);
            shopQrcodeModel.setImageUrl(QRCode);
            shopQrcodeModel.setIsDelete(false);
            shopQrcodeModel.setCreateBy(1L);
            shopQrcodeModel.setCreateTime(new Date());
            ShopQrcodeModel shopQrcodeModel1 = repository.save(shopQrcodeModel);
            if (shopQrcodeModel1 != null) {
                return new DataOutput(convertEntityToDto(shopQrcodeModel1));
            } else {
                return new DataOutput(CommonStatus.SUCCESS);
            }

        }
        return null;
    }

    @Transactional
    public DataOutput createQRCode(HttpServletRequest request, String url) {
        OperationUserCmsModel operationUserModel = UserSession.getUserSession(request);
        if (operationUserModel==null ||  operationUserModel.getShopId() == null) {
            return new DataOutput(CommonStatus.BAD_REQUEST.getCode(),"获取登录信息失败，请重新登录!");
        }
        if (operationUserModel.getShopId() != null && StringUtils.isNotBlank(url)) {
            ShopQrcodeModel shopQrcodeModel = this.findShopQRCodeByShopIdAndUrl(operationUserModel.getShopId(), url);
            if (shopQrcodeModel != null) {
                return new DataOutput(shopQrcodeModel);
            } else {
                try {
                    //生成二维码
                    String QRurl = qrCodeImageUtils.createQrCode(url, 200, "png");
                    if (StringUtils.isNotBlank(QRurl)) {
                        //上传服务器
                        DataOutput dataOutput = updateFileUtils.updateRuHuaFile(QRurl);
                        if (dataOutput.getStatus().equals(CommonStatus.SUCCESS.getCode())) {
                            JSONObject jsonObject = JSON.toJSONObject(dataOutput.getData());
                            String imageUrl = jsonObject.getString("imgUrl");
                            if (StringUtils.isNotBlank(imageUrl)) {
                                return this.saveShopQRCode(operationUserModel.getShopId(), url, imageUrl);
                            }
                        }
                    }
                } catch (WriterException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return new DataOutput(CommonStatus.SERVER_ERROR.getCode(), "操作失败请重新操作!");
            }
        } else {
            return new DataOutput(CommonStatus.BAD_REQUEST.getCode(),"用户未绑定门店或url为空!");
        }
    }
}
